<template>
	<div id='system' v-loading="loading">
		<div class="content-top">
			<span class="h1">审批查询</span>
			<!--			<span class="h2">审批查询</span>-->
		</div>
		<div class="top-btn clearfix">
			<div class="left-content">
				<span class="tip">需求处理查询</span>
				<el-radio-group v-model="rate" @change = "change()">
					<el-radio :label="1">审核中</el-radio>
					<el-radio :label="2">初审通过</el-radio>
					<el-radio :label="4">拒绝</el-radio>
					<el-radio :label="3">同意</el-radio>
					<!--<el-radio :label="5">已放款</el-radio>-->
				</el-radio-group>
			</div>
			<div class="right-search1">
				<el-input v-model="input" placeholder="输入关键词…"></el-input>
				<el-button type="primary" @click="look()" style="margin-left: 10px;">查看</el-button>
				<el-button type="primary" @click="execl()">导出excel</el-button>
			</div>
		</div>
		<el-table :data="tableData" style="width: 100%">
			<el-table-column label="姓名">
				<template slot-scope="scope">
					<span>{{ scope.row.MemberInfo.CNName }}</span>
				</template>
			</el-table-column>
			<el-table-column label="性别">
				<template slot-scope="scope">
					<span>{{ scope.row.MemberInfo.Gender }}</span>
				</template>
			</el-table-column>
			<el-table-column label="手机号">
				<template slot-scope="scope">
					<span v-if="scope.row.Status == 1">{{ (scope.row.MemberInfo.Tel).substr(0, 3) + '****' + (scope.row.MemberInfo.Tel).substr(7) }}</span>
					<span v-else>{{scope.row.MemberInfo.Tel}}</span>
				</template>
			</el-table-column>
			<el-table-column label="电话实名期限">
				<template slot-scope="scope">
					<span>{{ scope.row.MemberInfo.TelYears }}</span>
				</template>
			</el-table-column>
			<el-table-column label="住址">
				<template slot-scope="scope">
					<span>{{ scope.row.MemberInfo.Address }}</span>
				</template>
			</el-table-column>
			<el-table-column label="芝麻分">
				<template slot-scope="scope">
					<span>{{ scope.row.MemberInfo.ZMScore }}</span>
				</template>
			</el-table-column>
			<!--<el-table-column label="QQ">
				<template slot-scope="scope">
					<span v-if="scope.row.Status == 1">{{ (scope.row.MemberInfo.QQ).substr(0, 3) + '****' + (scope.row.MemberInfo.QQ).substr(7) }}</span>
					<span v-else>{{scope.row.MemberInfo.QQ}}</span>
				</template>
			</el-table-column>-->
			<el-table-column label="微信">
				<template slot-scope="scope">
					<span v-if="scope.row.Status == 1">{{ (scope.row.MemberInfo.WX).substr(0, 3) + '****' + (scope.row.MemberInfo.WX).substr(7) }}</span>
					<span v-else>{{scope.row.MemberInfo.WX}}</span>
				</template>
			</el-table-column>
			<!--<el-table-column label="联系人">
				<template slot-scope="scope">
					<span>{{scope.row.MemberInfo.EmergencyName}}</span>
				</template>
			</el-table-column>
			<el-table-column label="联系人电话">
				<template slot-scope="scope">
					<span>{{scope.row.MemberInfo.EmergencyPhone}}</span>
				</template>
			</el-table-column>
			<el-table-column label="联系人关系">
				<template slot-scope="scope">
					<span>{{scope.row.MemberInfo.EmergencyRelation}}</span>
				</template>
			</el-table-column>-->
			<el-table-column prop="RefuesMemo" label="拒绝原因">
			</el-table-column>
			<el-table-column prop="StatusStr" label="需求状态">
			</el-table-column>
			<el-table-column label="风控报告">
		      <template slot-scope="scope">
		      		<router-link v-bind="{to:'/creditInfos/'+(scope.row.MemberID)}"><el-button size="mini">风控报告  </el-button></router-link>
		      </template>
		    </el-table-column>
			<el-table-column label="审核" >
				<template slot-scope="scope">
					<el-dropdown trigger="click" v-if="scope.row.Status != 4">
						<span class="el-dropdown-link"><el-button type="primary">审核<i class="el-icon-arrow-down el-icon--right"></i></el-button></span>
						<el-dropdown-menu slot="dropdown" class="div">
							<el-dropdown-item>
								<!--<el-button size="small" type="primary" @click="agree(scope.row.ID)">复审</el-button>-->
								<router-link v-if="scope.row.Status == 1"  v-bind="{to:'/deneeds'}"><el-button size="small"  type="primary">初审</el-button></router-link>
							</el-dropdown-item>
							<el-dropdown-item>
								<!--<el-button size="small" type="primary" @click="agree(scope.row.ID)">复审</el-button>-->
								<router-link v-if="scope.row.Status == 2"  v-bind="{to:'/rehear/'+(scope.row.ID)}"><el-button size="small"  type="primary">复审</el-button></router-link>
							</el-dropdown-item>
							<el-dropdown-item>
								<el-button size="small" type="warning" @click="refuse(scope.row.ID)">拒绝</el-button>
							</el-dropdown-item>
							<el-dropdown-item>
								<el-button size="small" type="info" v-if="scope.row.Status == 3" @click="Loan(scope.row.MemberID,scope.row.MDemandID,scope.row.ProviderID,scope.row.ID)">放款</el-button>
							</el-dropdown-item>
							<!--<el-dropdown-item>
								<el-button size="small" type="danger" @click="del(scope.row.ID)">删除</el-button>
							</el-dropdown-item>-->
							<!--<el-dropdown-item>
								<el-button size="small" type="primary">详情</el-button>
							</el-dropdown-item>-->
						</el-dropdown-menu>
					</el-dropdown>
				</template>
			</el-table-column>
		</el-table>
		<div class="pages">
			<el-pagination small :current-page="index" @current-change="handleCurrentChange" layout="prev, pager, next" :total="total" background prev-text='上一页' next-text='下一页'>
			</el-pagination>
		</div>
		<el-dialog title="审批管理" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
			<p class="total">拒绝原因：</p>
			<div >
				<el-input type="textarea" v-model="desc"></el-input>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button @click="quxia()">取 消</el-button>
				<el-button type="primary" @click="decline()">确 定</el-button>
			</span>
		</el-dialog>
		<!--放款-->
		<el-dialog title="新建放款" :visible.sync="mark" width="30%" :before-close="handleClose">
			<el-form ref="form" :model="form" label-width="100px">
				<el-form-item label="放款日期">
					<el-date-picker v-model="datatimes" type="date"  format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" placeholder="选择日期">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="放款期限">
					<el-input v-model="form.days" @change="add()" style="width: 70%;"></el-input>&nbsp;&nbsp;天
				</el-form-item>
				<el-form-item label="还款日期">
					<el-date-picker v-model="endtimes"  :disabled='true' type="date" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" placeholder="选择日期">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="放款金额">
					<el-input v-model="form.money"></el-input>
				</el-form-item>
				<el-form-item label="利息">
					<el-input v-model="Interest"></el-input>
				</el-form-item>
				<el-form-item label="借条平台">
					<el-select v-model="value" placeholder="请选择">
						<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="对方账号">
					<el-input v-model="form.member"></el-input>
				</el-form-item>
				<el-form-item label="放款账号">
					<el-input v-model="form.refund"></el-input>
				</el-form-item>
				<el-form-item label="备注">
					<el-input v-model="Memo"></el-input>
				</el-form-item>
				<el-form-item label="审核人">
					<el-input v-model="form.men"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="quxia()">取 消</el-button>
				<el-button type="primary" @click="fangkuan()">确定放款</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import apis from '../../../api/prouserApi'
	export default {
		name: 'examine',
		data() {
			return {
				loading:true,
				mark:false,
				dialogVisible:false,
				rate: 2, //分配
				input: '', //搜索
				tableData: [], //列表
				ProviderID:'',
				index:1,
				total:1,
				id:'',
				desc:'',
				datatimes:'',//放款日期
				endtimes:"",//还款日期
				Interest:'',//利息
				Memo:'',//备注
				form:{
					days:'',
					money:"",//放款金额
					member:"",//对方账号
					refund:"",//放款账号
					men:sessionStorage.CNName,//收款负责人
				},
				options: [{
					value: '今借到',
					label: '今借到'
				}, {
					value: '米房',
					label: '米房'
				}, {
					value: '借贷宝',
					label: '借贷宝'
				},{
					value: '无忧借条',
					label: '无忧借条'
				},{
					value: '安牛有借',
					label: '安牛有借'
				}],
				value:'',//渠道
			}
		},
		created() {
			this.ProviderID = sessionStorage.ProviderID
			this.allotlist(this.index)
		},
		methods: {
			execl(){
				this.loading = true
				this.$axios.post(apis.allotlist,{
				  "ProviderID": this.ProviderID,
				  "Status": this.rate,
				  "pagePara": {
				    "Para": this.input,
				    "PageSize": 10,
				    "PageIndex": this.index-1,
				    "OrderbyString": "CNName",
				     "ToExcel": true
				  }
				}).then(res=>{
					if(res.data.isOK){
						if(res.data.data.ExcelResult && res.data.data.ExcelResult.isOK) {
							this.loading = false
							var urls = res.data.data.ExcelResult.data.filePath
							window.location.href = apis.host+'/template/'+urls
						} else{
							this.loading = false
							this.$message.error(res.data.data.ExcelResult.msg);
						}
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			handleEdit(index, row) {
				console.log(index, row);
			},
			handleDelete(index, row) {
				console.log(index, row);
			},
			handleClose(){
				this.quxia()
			},
			quxia(){
				this.mark = false
				this.dialogVisible = false
			},
			//分页
			handleCurrentChange(val){
				this.index = val
				this.allotlist(this.index)
			},
			look(){
				this.index = 1
				this.allotlist(this.index)
			},
			//获取审批列表
			allotlist(val){
				this.loading = true
				this.$axios.post(apis.allotlist,{
				  "ProviderID": this.ProviderID,
				  "Status": this.rate,
				  "pagePara": {
				    "Para": this.input,
				    "PageSize": 10,
				    "PageIndex": val-1,
				    "OrderbyString": "CNName",
				     "ToExcel": false
				  }
				}).then(res=>{
					if(res.data.isOK) {
						this.loading = false
						this.tableData = res.data.data.Data
						this.total = res.data.data.Items?res.data.data.Items:1
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			agree(id){
				this.examine(id,3,'')	
			},
			//审批
			examine(id,sts,conten){
				this.loading = true
				this.$axios.post(apis.setallot,{
				  "AllotID": id,
				  "Status": sts,
				  "RefuseMemo":conten
				}).then(res=>{
					if(res.data.isOK) {
						this.loading = false
						this.dialogVisible = false
						this.allotlist(this.index)
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			
			//拒绝
			refuse(id){
				this.desc=""
				this.dialogVisible = true
				this.id = id
			},
			//确认拒绝
			decline(){
				this.examine(this.id,4,this.desc)
			},
			//弹框放款
			Loan(MemberID,MDemandID,ProviderID,id){
				this.MemberID = MemberID
				this.MDemandID = MDemandID
				this.ProviderID = ProviderID
				this.id = id
				this.mark = true
			},
			add(){
				this.endtimes = new Date(this.form.days*86400000 + new Date(this.datatimes).getTime())
			},
			//放款
			fangkuan(){
				this.loading = true
				this.$axios.post(apis.addloan,{
				  "AllotID": this.id,
				  "LoanDate":this.datatimes ,
				  "RefundDate": this.endtimes,
				  "LoanMoney": this.form.money,
				  "LoanAccountType": this.value,
				  "MemberAccountCode": this.form.member,
				  "LoanAccountCode": this.form.refund,
				  "RefundMan": this.form.men,
				  "DemandID": this.MDemandID,
				  "ProviderID": this.ProviderID,
				  "MemberID": this.MemberID,
				  "Interest": this.Interest,
				  "Memo": this.Memo,
				}).then(res=>{
					if(res.data.isOK) {
						this.loading = false
						this.mark = false
						this.allotlist(this.index)
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
			//状态改变
			change(){
				this.loading = true;
				this.index = 1
				this.$axios.post(apis.allotlist,{
				  "ProviderID": this.ProviderID,
				  "Status": this.rate,
				  "pagePara": {
				    "Para": "",
				    "PageSize": 10,
				    "PageIndex": this.index-1,
				    "OrderbyString": "CNName",
				     "ToExcel": false
				  }
				}).then(res=>{
					if(res.data.isOK) {
						this.loading = false
						this.tableData = res.data.data.Data
						this.total = res.data.data.Items?res.data.data.Items:1
					} else {
						if(res.data.msg == "Token无效" || res.data.msg == "Token缺失") {
							this.$router.push({
								path: '/'
							})
						} else {
							this.loading = false
							this.$message.error(res.data.msg);
						}
					}
				})
			},
		}
	}
</script>

<style>

</style>